package com.nuonuo.es.controller;

import com.alibaba.fastjson.JSON;
import com.nuonuo.es.service.ElinInvoiceCodeNumService;
import com.nuonuo.es.vo.InvoiceCodeNumberIndexVO;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author duan
 * @version 1.0
 * @date 2023/8/30 17:53
 */
@RestController
@RequestMapping(value = "/invoice")
public class ElinInvoiceCodeNumController {

    @Autowired
    private ElinInvoiceCodeNumService elinInvoiceCodeNumService;

    @GetMapping(value = "/search")
    public List<Map<String, Object>> search(String invoiceCode, String invoiceNum) {
        SearchResponse response;
        if (StringUtils.isBlank(invoiceNum)) {
            response = elinInvoiceCodeNumService.search(invoiceCode);
            System.out.println("size = " + response.getHits().getTotalHits());
        } else {
            response = elinInvoiceCodeNumService.search(invoiceCode, invoiceNum);
        }
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        System.out.println(JSON.toJSON(response.getHits().getHits()[0].getSourceAsMap()));
        return result;
    }

    @GetMapping(value = "/searchByBizId")
    public List<Map<String, Object>> searchByBizId(String invoiceCode, String invoiceNum) {
        String id = invoiceCode + invoiceNum;
        SearchResponse response = elinInvoiceCodeNumService.searchByBizId(id);
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        System.out.println(JSON.toJSON(response.getHits().getHits()[0].getSourceAsMap()));
        return result;
    }


    @GetMapping(value = "/searchById")
    public List<Map<String, Object>> searchById(String invoiceCode, String invoiceNum) {
        String id = invoiceCode + invoiceNum;
        SearchResponse response = elinInvoiceCodeNumService.searchById(id);
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        System.out.println(JSON.toJSON(response.getHits().getHits()[0].getSourceAsMap()));
        return result;
    }

    @GetMapping(value = "/searchByIdRouting")
    public List<Map<String, Object>> searchByIdRouting(String invoiceCode, String invoiceNum) {
        String id = invoiceCode + invoiceNum;
        SearchResponse response = elinInvoiceCodeNumService.searchByIdRouting(id);
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        System.out.println(JSON.toJSON(response.getHits().getHits()[0].getSourceAsMap()));
        return result;
    }

    @GetMapping(value = "/searchByRouting")
    public List<Map<String, Object>> searchByRouting(String invoiceCode, String invoiceNum) {
        String id = invoiceCode + invoiceNum;
        SearchResponse response = elinInvoiceCodeNumService.searchByRouting(id);
        List<Map<String, Object>> result = new ArrayList<>();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        System.out.println(JSON.toJSON(response.getHits().getHits()[0].getSourceAsMap()));
        return result;
    }


    @GetMapping(value = "/get")
    public Map<String, Object> get(String invoiceCode, String invoiceNum) {
        GetResponse response =  elinInvoiceCodeNumService.get(invoiceCode, invoiceNum);
        return response.getSourceAsMap();
    }

    @GetMapping(value = "/getMulti")
    public List<Map<String, Object>> getMulti(String invoiceCode, String invoiceNum) {
        return elinInvoiceCodeNumService.getMulti(invoiceCode, invoiceNum);
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody InvoiceCodeNumberIndexVO vo) {
        elinInvoiceCodeNumService.save(vo);
        return "OK";
    }

    @PostMapping(value = "/delete")
    public String delete(String invoiceCode, String invoiceNum) {
        elinInvoiceCodeNumService.delete(invoiceCode, invoiceNum);
        return "OK";
    }
}
